package com.tqyan.start.controller;

import com.tqyan.service.transfer.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @description 会话API
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @date 2023/4/11 17:20
 */
@RestController
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @RequestMapping("/paper/download/{fileName}")
    public Object paperDownload(HttpServletResponse response, @PathVariable String fileName) {
        try {
            String downloadName = fileName.split("_")[0] + "." + fileName.split("\\.")[1];
            response.setHeader("content-disposition", "attachment;filename="
                    + URLEncoder.encode(downloadName, StandardCharsets.UTF_8));
            fileService.downloadAliyunOSS(response.getOutputStream(), FileService.FOLDER_PAPER, fileName);
            return null;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @RequestMapping("/novel/download/{fileName}")
    public Object novelDownload(HttpServletResponse response, @PathVariable String fileName) {
        try {
            String downloadName = fileName.split("_")[0] + "." + fileName.split("\\.")[1];
            response.setHeader("content-disposition", "attachment;filename="
                    + URLEncoder.encode(downloadName, StandardCharsets.UTF_8));
            fileService.downloadAliyunOSS(response.getOutputStream(), FileService.FOLDER_NOVEL, fileName);
            return null;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

}


